/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.ASColor;
import org.aswing.dnd.DraggingImage;
import org.aswing.ElementCreater;
import org.aswing.graphics.Graphics;
import org.aswing.graphics.Pen;

import fever.utils.Stringifier;

import fvaswing.components.FvTree;

/**
 * Image for {@link fvaswing.components.FvTree} dragging process.
 * 
 * @author Romain Ecarnot
 */
class fvaswing.components.tree.FvTreeDragImage implements DraggingImage
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
		
	private var _tree : FvTree;
	private var _textField : TextField;
	private var _width : Number;
	private var _height : Number;
	private var _target : MovieClip;
	private var _number : Number;
	private var _x : Number;
	private var _y : Number;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor
	 */
	public function FvTreeDragImage( t : FvTree, offsetY : Number )
	{
		_height = t.getRowHeight();
		
		if( _height == null ) _height = 18;
		
		if( t.getFixedCellWidth() > 10 )
		{
			_width = t.getFixedCellWidth();
		}
		else _width = t.getInsets().getInsideSize( t.getSize() ).width - 10;
		
		_number = t.getSelectionPaths().length;
		_x = 0;
		_y = offsetY;
	}
	
	/**
	 * Paints the image for normal state of dragging.
	 */
	public function setCanvas( target : MovieClip ) : Void
	{
		_target = target;
		_textField.removeTextField();
		_textField = ElementCreater.getInstance().createTF( target, "n_of_draging_text" );
	}
	
	/**
	 * Paints the image for accept state of dragging.(means drop allowed)
	 */
	public function switchToAcceptImage() : Void
	{
		_target.clear();
		drawItems( new Graphics( _target ), true );
	}
	
	/**
	 * Paints the image for reject state of dragging.(means drop not allowed)
	 */
	public function switchToRejectImage() : Void
	{
		_target.clear();
		drawItems( new Graphics( _target ), false );
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
		
	private function drawItems( g : Graphics, allow : Boolean ) : Void
	{
		var w : Number = _width;
		var h : Number = _height;
		var r : Number = Math.min(w, h) - 2;
		
		if( !allow )
		{
			g.drawLine( new Pen( ASColor.RED, 2), _x + 1, _y + 1, _x + 1 + r, _y + 1 + r );
			g.drawLine( new Pen( ASColor.RED, 2), _x + 1 + r, _y + 1, _x + 1, _y + 1 + r );
		}
		
		_textField._visible = false;
		
		if( _number > 1 )
		{
			if( _number > 2 )
			{
				g.drawRectangle( new Pen( ASColor.GRAY ), _x + 4, _y + 4, w-3, h-2 );
				_textField._visible = true;
				_textField._x = _x + _width + 2;
				_textField._y = _y;
				_textField.text = ( _number + "" );
			}
			
			g.drawRectangle( new Pen( ASColor.GRAY ), _x + 2, _y + 2, w-1, h-1 );
		}
		
		g.drawRectangle( new Pen( ASColor.GRAY ), _x, _y, w, h );
	}
}